(function(window) {

	function isEmpty(obj) {
		if (typeof obj == 'undefined' || obj === null || obj === '')
			return true;
		if (typeof obj == 'number' && isNaN(obj))
			return true;
		if (obj instanceof Date && isNaN(Number(obj)))
			return true;
		return false;
	}

	function hexFromRGB(r, g, b) {
		var hex = [ r.toString(16), g.toString(16), b.toString(16) ];
		$.each(hex, function(nr, val) {
			if (val.length === 1) {
				hex[nr] = "0" + val;
			}
		});
		return hex.join("").toUpperCase();
	}
	function refreshSwatch() {
		var red = $(constants_id_input_color_red).slider("value"), green = $(
				constants_id_input_color_green).slider("value"), blue = $(
				constants_id_input_color_blue).slider("value"), hex = hexFromRGB(
				red, green, blue);
		$(constants_id_input_color_slide).css("background-color", "#" + hex);
	}

	function insertText() {
		var text = $(constants_id_input_text).val();
		var size = $(constants_id_input_size_slide).slider("value");
		var font = $(constants_id_input_font).val();

		var red = $(constants_id_input_color_red).slider("value"), green = $(
				constants_id_input_color_green).slider("value"), blue = $(
				constants_id_input_color_blue).slider("value"), hex = hexFromRGB(
				red, green, blue);

		if (isEmpty(size)) {
			size = 50;
		}
		addText(100, 100, text, '#' + hex, size + 'px ' + font, 'bottom', true);
	}

	function saveText() {
		canvas = document.getElementById(constants_id_graph);
		var strDataURI = canvas.toDataURL(); 
		window.open(canvas.toDataURL("image/png"));
		//alert(strDataURI);
	}

	function loadSizeSlider() {
		$(constants_id_input_size_slide).slider({
			range : "min",
			value : 50,
			min : 1,
			max : 100,
			slide : function(event, ui) {
				$(constants_id_slide_output).text(ui.value);
			}
		});
		$(constants_id_slide_output).text(
				$(constants_id_input_size_slide).slider("value"));
	}

	function loadColorSlider() {

		var colors = constants_id_input_color_red + ', '
				+ constants_id_input_color_green + ', '
				+ constants_id_input_color_blue;
		$(colors).slider({
			orientation : "horizontal",
			range : "min",
			max : 255,
			value : 127,
			slide : refreshSwatch,
			change : refreshSwatch
		});
		$(constants_id_input_color_red).slider("value", 255);
		$(constants_id_input_color_green).slider("value", 140);
		$(constants_id_input_color_blue).slider("value", 60);
	}

	function loadAccordion() {

		var allPanels = $('.accordion > div.content-accordion').hide();

		$('div.accordion > div.header-accordion > a').click(function() {
			$this = $(this);
			var target = $this.parent().next();

			if (!target.hasClass('active')) {
				allPanels.removeClass('active').slideUp();
				target.addClass('active').slideDown();
			}

			return false;
		});

	}

	window.loadSizeSlider = loadSizeSlider;
	window.insertText = insertText;
	window.loadColorSlider = loadColorSlider;
	window.loadAccordion = loadAccordion;
	window.saveText = saveText;

})(window);
